// VideoPlayer.cpp: implementation of the CVideoPlayer class.
//
//////////////////////////////////////////////////////////////////////
#ifdef VC70

#include "stdafx.h"

#include "../Logrus/Codec.h"
#include "../Common/CTags.h"
#include "VideoPlayer.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CVideoPlayer::CVideoPlayer()
{
	m_lTotalStoredData = 0;
	m_lTotalPlayData = 0;
	m_lCurrentStoredData = 0;
	m_file = NULL;
	m_pData = new BYTE[0x200000];
	slFile=0;
	m_nFastSpeed = 4;
	m_nCurrentFrameRate = 15;

	timeBegin = 0;
	timeEnd = 0;
	timeFrame = 0;
	dwDelta = 0;
	nFPS = 0;
	
	m_nTotalSteps = 0;

	m_pAudio[0] = m_AudioData;
	m_pAudio[1] = m_AudioData+(PACKET_SIZE*100);
	memset(m_pAudio[1], 0, PACKET_SIZE*100);
	m_flip = 0;
}

CVideoPlayer::~CVideoPlayer()
{
	Terminate(TRUE);
	if(m_file)		
	{
		fclose(m_file);
		m_file = NULL;
	}

	if(m_pData)		delete[] m_pData;
	if(slFile && slFile->IsLocked()) slFile->Unlock();
	if(slFile)		delete slFile;
}

DWORD CVideoPlayer::OnTerminate()
{
	CEvent* pevent = getRefreshEvent();
	if(pevent) pevent->SetEvent();
	return CRMSThread::OnTerminate();
}

DWORD CVideoPlayer::ThreadFunc()
{
	mesOut(_T("VideoPlayer.cpp"), __LINE__, _T("entering ThreadFunc(), m_bQuit=%i"), m_bQuit);

	if(!slFile)
	{
		CSyncObject* flock = getFileLock();
		slFile = (flock) ? new CSingleLock(flock) : NULL;
	}
	if(slFile && slFile->IsLocked()) slFile->Unlock();

	if(m_file)
	{
		fclose(m_file);
		m_file = NULL;
	}
open_file:
	CString hp = getHomePath();
	hp += "\\";
	hp += STORED_VIDEO_FILENAME;
	m_file = fopen(hp, "rb");
	if(!m_file)
	{
		if(m_bQuit) goto error_exit;
		Sleep(200);
		goto open_file;
	}

	m_eventSync.ResetEvent();

	m_lCurrentPlayData = 0;
	while(!m_bQuit)
	{
		switch(m_dwCommand)
		{
		case COMMAND_PAUSE:
			Sleep(1);
			break;
		case COMMAND_PLAY:
			if(m_lCurrentPlayData >= (m_lCurrentStoredData - 1)) 
			{
//				SetMode(COMMAND_STOP);
//				SetMode(COMMAND_PAUSE);
				Sleep(1);
				break;
			}
			if(!GetFrameFromFile())	goto error_exit;

			timeFrame = (DWORD)((double)1000 / (double)m_nFrameRate);
			timeEnd = timeGetTime();
			if(timeFrame > (timeEnd - timeBegin))
			{
				Sleep(timeFrame - (timeEnd - timeBegin));
				dwDelta += timeGetTime() - timeBegin;
			}
			else
			{
				timeFrame = timeEnd - timeBegin;
				dwDelta += timeFrame;
			}
			timeBegin = timeGetTime();

			if(dwDelta > 1000)
			{
				m_nCurrentFrameRate = (int)(((float)nFPS * 1000.0) / (float)dwDelta + .5);
				m_nBitrate = (int)(((((float)m_lTotalPlayData * 8.0) / 1024.0) * 1000.0) / (float)dwDelta + .5);
				nFPS = 0;
				m_lTotalPlayData = 0;
				dwDelta = 0;
			}
			nFPS++;
			break;
		case COMMAND_STOP:
			fseek(m_file, 0L, SEEK_SET);
			if(!GetFrameFromFile())	goto error_exit;
			fseek(m_file, 0L, SEEK_SET);
			m_lCurrentPlayData = 0;
			m_dwCommand = COMMAND_PAUSE;
			m_dwOldCommand = COMMAND_PAUSE;
			break;
		case COMMAND_BEGIN:
			fseek(m_file, 0L, SEEK_SET);
			if(!GetFrameFromFile())	goto error_exit;
			fseek(m_file, 0L, SEEK_SET);
			m_lCurrentPlayData = 0;
			m_dwCommand = m_dwOldCommand;
			break;
		case COMMAND_END:
			if(!SeekFromFile(m_lCurrentStoredData)) goto error_exit;
			m_dwCommand = COMMAND_PAUSE;
			m_dwOldCommand = COMMAND_PAUSE;
			break;
		case COMMAND_FAST:
			if(m_lCurrentPlayData >= (m_lCurrentStoredData-1)) 
			{
//				SetMode(COMMAND_STOP);
				break;
			}
			if(!FastForwardStep(m_nFastSpeed))	goto error_exit;
			break;
		case COMMAND_REWIND:
			if(m_lCurrentPlayData <= 1) 
			{
				SetMode(COMMAND_STOP);
				break;
			}
			if(!RewindStep(m_nFastSpeed))	goto error_exit;
			break;
		case COMMAND_SEEK:
			SeekFromFile(m_lNewPlayData);
//			((CStoredPage*)dlg->m_RMSTab.m_RMSTabPages[1])->m_bSeekSlider = FALSE;
			m_dwCommand = m_dwOldCommand;
			break;
		default:
			Sleep(1);
			break;
		}
	}
error_exit:
	if(m_file)
	{
		fclose(m_file);
		m_file = NULL;
	}

	m_eventSync.ResetEvent();

//	OnConnectFailed(m_nCamera, 0);
	m_bTerminated=TRUE;

	mesOut(_T("VideoPlayer.cpp"), __LINE__, _T("leaving ThreadFunc()"));

	return 0;
}

DWORD CVideoPlayer::StartPlayer(int nW, int nH)
{
	m_nW = nW;
	m_nH = nH;
	return (Execute()) ? 1 : 0;
}

DWORD CVideoPlayer::StopPlayer(BOOL bCritical)
{
	DWORD result;
	if(m_dwCommand != COMMAND_SEEK)
	{
		m_dwOldCommand = m_dwCommand;
	}
	m_dwCommand = COMMAND_NONE;

	m_eventSync.ResetEvent();

	result = Terminate(bCritical);

	if(slFile && slFile->IsLocked()) slFile->Unlock();

	if(m_file)
	{
		fclose(m_file);
		m_file = NULL;
	}

	m_lTotalStoredData = 0;
	m_lCurrentStoredData = 0;
	
	return result;
}

void CVideoPlayer::SetMode(DWORD dwCommand)
{
	if(m_dwCommand != COMMAND_SEEK)
	{
		m_dwOldCommand = m_dwCommand;
	}
	m_dwCommand = dwCommand;
}

BOOL CVideoPlayer::GetFrameFromFile()
{
	DWORD dwRead; 
	DWORD dwLen;
	DWORD dwTag;
	fpos_t pos;
	fpos_t tp;
	BOOL eof;

	BOOL flag = FALSE;

	BOOL result = FALSE;

	slFile->Lock();
	fgetpos(m_file, &pos);
	eof = feof(m_file);
	slFile->Unlock();

	while(!m_bQuit && !eof && !result)
	{
		slFile->Lock();
		dwTag = 0;
		fread(&dwTag, 4, 1, m_file);
		if(eof = feof(m_file)) 
		{
			slFile->Unlock();
			break;
		}
		m_lTotalPlayData += sizeof(DWORD); 

		switch(dwTag)
		{
		case TAG_SIZE:
			fgetpos(m_file, &tp); tp += sizeof(DWORD); fsetpos(m_file, &tp);
			if(eof = feof(m_file)) break;
			m_lTotalPlayData += sizeof(DWORD); 
			break;
		case TAG_STOREDDATA:
			if(flag)
			{
				fgetpos(m_file, &tp); tp -= 4; fsetpos(m_file, &tp);
				if(eof = feof(m_file)) break;
				result = TRUE;
			}
			else
			{
				flag = TRUE;
			}
			m_lTotalPlayData += sizeof(DWORD); 
			break;
		case TAG_TIMEMARK:
			fgetpos(m_file, &tp); tp += sizeof(TimeMark); fsetpos(m_file, &tp);
			if(eof = feof(m_file)) break;
			m_lTotalPlayData += sizeof(TimeMark) + sizeof(DWORD); 
			break;
		case TAG_RMSSTOREDVIDEO_v01:
			dwLen = 0;
			fread(&dwLen, sizeof(DWORD), 1, m_file);
			if(eof = feof(m_file)) break;
			fread(&m_nFrameRate, sizeof(int), 1, m_file);
			if(eof = feof(m_file)) break;
			dwRead = fread(m_pData, 1, dwLen-4, m_file);
			if(eof = feof(m_file)) break;
			if(dwRead != (dwLen - 4)) break;
			if(m_bQuit) break;
			if(dwLen > 0)
			{
				extern BYTE* CMemory;
				enterCritical();
				BYTE** py = getYComponent();
				BYTE** pu = getUComponent();
				BYTE** pv = getVComponent();
				DECOMPRESS_FRAME(*py, *pu, *pv, m_nW, m_nH, m_pData, CMemory);
				leaveCritical();
				if(WaitForRefresh() == 0) 
				{
					slFile->Unlock();
					return FALSE;
				}
			}

			m_lTotalPlayData += dwLen + sizeof(DWORD); 
			break;
		case TAG_STOREDTIME_v01:
			dwLen = 0;
			fread(&dwLen, sizeof(DWORD), 1, m_file);
			if(eof = feof(m_file)) break;
			if(dwLen > 50)
			{
				slFile->Unlock();
				return FALSE;
			}
			dwRead = fread(m_szTime, 1, dwLen, m_file);
			if(eof = feof(m_file)) break;
			if(dwRead != (dwLen)) break;
			enterCritical();
			setSessionTime(m_szTime);
			leaveCritical();
			m_lTotalPlayData += dwLen + sizeof(DWORD); 
			break;
		case TAG_STOREDDATE_v01:
			dwLen = 0;
			fread(&dwLen, sizeof(DWORD), 1, m_file);
			if(eof = feof(m_file)) break;

			if(dwLen > 50)
			{
				slFile->Unlock();
				return FALSE;
			}
			dwRead = fread(m_szDate, 1, dwLen, m_file);
			if(eof = feof(m_file)) break;
			if(dwRead != (dwLen)) break;
			enterCritical();
			setSessionDate(m_szDate);
			leaveCritical();
			m_lTotalPlayData += dwLen + sizeof(DWORD); 
			break;
		case TAG_RMSSTOREDSOUND_v01:
			dwLen = 0;
			fread(&dwLen, sizeof(DWORD), 1, m_file);
			if(eof = feof(m_file)) break;
			dwRead = fread(m_pAudio[m_flip], 1, dwLen, m_file);
			if(eof = feof(m_file)) break;
			if(dwRead != dwLen) break;

			if(m_bQuit) break;
			if(dwLen > 0)
			{
				m_flip ^= 1;
				outAudio(m_pAudio[m_flip^1], dwLen);
			}

			m_lTotalPlayData += dwLen + sizeof(DWORD); 
			break;
		default:
			dwLen = 0;
			fread(&dwLen, sizeof(DWORD), 1, m_file);
			if(eof = feof(m_file)) break;
			fgetpos(m_file, &tp); tp += dwLen; fsetpos(m_file, &tp);
			if(eof = feof(m_file)) break;
		}
		if( !eof )
		{
			fgetpos(m_file, &pos);
		}
		slFile->Unlock();
	}
	slFile->Lock();
	if( eof ) 
	{
		clearerr(m_file);
//		if(m_dwCommand != COMMAND_STOP)
//		{
//			SetMode(COMMAND_STOP);
//			SetMode(COMMAND_PAUSE);
//		}
		result = TRUE;

	}
	fsetpos(m_file, &pos);
	slFile->Unlock();
	m_lCurrentPlayData = pos;
	return result;
}

BOOL CVideoPlayer::FastForwardStep(int nMaxStep)
{
	DWORD dwRead;
	DWORD dwLen;
	DWORD dwTag;
	fpos_t pos, tp;
	BOOL eof;

	BOOL flag = FALSE;
	BOOL result = FALSE;

	slFile->Lock();
	fgetpos(m_file, &pos);
	eof = feof(m_file);
	slFile->Unlock();

	if(m_nTotalSteps >= nMaxStep)
	{
		m_nTotalSteps = 0;
		while(!m_bQuit && !eof && !result)
		{
			slFile->Lock();
			dwTag = 0;
			fread(&dwTag, 4, 1, m_file);
			if(eof = feof(m_file))
			{
				slFile->Unlock();
				break;
			}
			switch(dwTag)
			{
			case TAG_SIZE:
				fgetpos(m_file, &tp); tp += sizeof(DWORD); fsetpos(m_file, &tp);
				eof = feof(m_file);
				break;
			case TAG_STOREDDATA:
				if(flag)
				{
					fgetpos(m_file, &tp); tp -= 4; fsetpos(m_file, &tp);
					if(eof = feof(m_file)) break;
					result = TRUE;
				}
				else
				{
					flag = TRUE;
				}
				break;
			case TAG_TIMEMARK:
				fgetpos(m_file, &tp); tp += sizeof(TimeMark); fsetpos(m_file, &tp);
				eof = feof(m_file);
				break;
			case TAG_RMSSTOREDVIDEO_v01:
				dwLen = 0;
				fread(&dwLen, sizeof(DWORD), 1, m_file);
				if(eof = feof(m_file)) break;
				fread(&m_nFrameRate, sizeof(int), 1, m_file);
				if(eof = feof(m_file)) break;
				dwRead = fread(m_pData, 1, dwLen-4, m_file);
				if(eof = feof(m_file)) break;
				if(dwRead != (dwLen - 4)) break;
				if(m_bQuit) break;
				if(dwLen > 0)
				{
					extern BYTE* CMemory;
					enterCritical();
					BYTE** py = getYComponent();
					BYTE** pu = getUComponent();
					BYTE** pv = getVComponent();
					DECOMPRESS_FRAME(*py, *pu, *pv, m_nW, m_nH, m_pData, CMemory);
					leaveCritical();
					if(WaitForRefresh() == 0) 
					{
						slFile->Unlock();
						return FALSE;
					}
				}
				break;
			case TAG_STOREDTIME_v01:
				dwLen = 0;
				fread(&dwLen, sizeof(DWORD), 1, m_file);
				if(eof = feof(m_file)) break;
				if(dwLen > 50)
				{
					slFile->Unlock();
					return FALSE;
				}
				dwRead = fread(m_szTime, 1, dwLen, m_file);
				if(eof = feof(m_file)) break;
				if(dwRead != (dwLen)) break;
				enterCritical();
				setSessionTime(m_szTime);
				leaveCritical();
				break;
			case TAG_STOREDDATE_v01:
				dwLen = 0;
				fread(&dwLen, sizeof(DWORD), 1, m_file);
				if(eof = feof(m_file)) break;
				if(dwLen > 50)
				{
					slFile->Unlock();
					return FALSE;
				}
				dwRead = fread(m_szDate, 1, dwLen, m_file);
				if(eof = feof(m_file)) break;
				if(dwRead != (dwLen)) break;
				enterCritical();
				setSessionDate(m_szDate);
				leaveCritical();
				break;
			default:
				dwLen = 0;
				fread(&dwLen, sizeof(DWORD), 1, m_file);
				if(eof = feof(m_file)) break;
				fgetpos(m_file, &tp); tp += dwLen; fsetpos(m_file, &tp);
				if(eof = feof(m_file)) break;
			}
			if( !eof )
			{
				fgetpos(m_file, &pos);
			}
			slFile->Unlock();
		}
		slFile->Lock();
		if( eof )
		{
			clearerr(m_file);
			if(m_dwCommand != COMMAND_STOP)
			{
				SetMode(COMMAND_PAUSE);
			}
			slFile->Unlock();
			return TRUE;
		}
		fsetpos(m_file, &pos);
		slFile->Unlock();
	}
	else
	{
		while(!m_bQuit && !eof && !result)
		{
			slFile->Lock();
			dwTag = 0;
			fread(&dwTag, 4, 1, m_file);
			if(eof = feof(m_file))
			{
				slFile->Unlock();
				break;
			}
			switch(dwTag)
			{
			case TAG_SIZE:
				fgetpos(m_file, &tp); tp += sizeof(DWORD); fsetpos(m_file, &tp);
				eof = feof(m_file);
				break;
			case TAG_STOREDDATA:
				if(flag)
				{
					fgetpos(m_file, &tp); tp -= 4; fsetpos(m_file, &tp);
					if(eof = feof(m_file)) break;
					result = TRUE;
				}
				else
				{
					flag = TRUE;
				}
				break;
			case TAG_TIMEMARK:
				fgetpos(m_file, &tp); tp += sizeof(TimeMark); fsetpos(m_file, &tp);
				if(eof = feof(m_file)) break;
				break;
			default:
				dwLen = 0;
				fread(&dwLen, sizeof(DWORD), 1, m_file);
				if(eof = feof(m_file)) break;
				fgetpos(m_file, &tp); tp += dwLen; fsetpos(m_file, &tp);
				if(eof = feof(m_file)) break;
			}
			if( !eof )
			{
				fgetpos(m_file, &pos);
			}
			slFile->Unlock();
		}
		slFile->Lock();
		if( eof )
		{
			clearerr(m_file);
			if(m_dwCommand != COMMAND_STOP)
			{
				SetMode(COMMAND_PAUSE);
			}
			result = TRUE;
		}
		fsetpos(m_file, &pos);
		slFile->Unlock();
	}

	m_nTotalSteps++;
	m_lCurrentPlayData = pos;
	return result;
}

#define MAXTEST 0x4000
BOOL CVideoPlayer::SeekFromFile(fpos_t position)
{
	DWORD dwTag;
	DWORD dwLen;
	BOOL result = FALSE;
	fpos_t lCur;
	fpos_t lRead;
	fpos_t lFind;
	BYTE* ptr;

	slFile->Lock();
	fsetpos(m_file, &position);
	slFile->Unlock();

	while(!m_bQuit && !feof(m_file) && !result)
	{
		slFile->Lock();
		fgetpos(m_file, &lCur);
		lCur -= MAXTEST;
		if(lCur < 0) lCur = 0;
		fsetpos(m_file, &lCur);
		lRead = fread(m_pData, 1, MAXTEST, m_file);
		fsetpos(m_file, &lCur);
		slFile->Unlock();
		if(feof(m_file))
		{
			break;
		}

		lFind = 0;
		ptr = m_pData;

		while(!m_bQuit && (lFind < lRead) && !result)
		{
			dwTag = 0;
			memcpy(&dwTag, ptr, sizeof(DWORD));
			switch(dwTag)
			{
			case TAG_STOREDDATA:
				result = TRUE;
				break;
			case TAG_TIMEMARK:
				lFind += sizeof(TimeMark);
				ptr += sizeof(TimeMark);
				break;
			case TAG_SIZE:
				lFind += sizeof(DWORD);
				ptr += sizeof(DWORD);
				break;
			case TAG_RMSSTOREDVIDEO_v01:
			case TAG_STOREDTIME_v01:
			case TAG_STOREDDATE_v01:
				dwLen = 0;
				lFind += sizeof(DWORD);
				ptr += sizeof(DWORD);
				memcpy(&dwLen, ptr, sizeof(DWORD));
				lFind += sizeof(DWORD) + dwLen;
				ptr += sizeof(DWORD) + dwLen;
				break;
			default:
				lFind += 1;
				ptr += 1;
				break;
			}
		}
		if(result)
		{
			slFile->Lock();
			fpos_t pos = lCur + lFind;
			fsetpos(m_file, &pos);
			slFile->Unlock();
		}
	}
	if(result)
	{
		result = GetFrameFromFile();
	}

	slFile->Lock();
	fgetpos(m_file, &m_lCurrentPlayData);
	slFile->Unlock();

	return result;
}

BOOL CVideoPlayer::RewindStep(int nMaxStep)
{
	fpos_t tp = 0;
	DWORD dwRead; 
	DWORD dwLen;
	DWORD dwTag;

	BOOL flag = FALSE;
	BOOL result = FALSE;

	if(m_nTotalSteps >= nMaxStep)
	{
		m_nTotalSteps = 0;

		slFile->Lock();
		fgetpos(m_file, &tp); tp -= 4; fsetpos(m_file, &tp);
		fread(&dwLen, sizeof(DWORD), 1, m_file);
		fgetpos(m_file, &tp); tp -= sizeof(DWORD)*2 + dwLen; fsetpos(m_file, &tp);
		slFile->Unlock();

		while(!m_bQuit && !feof(m_file) && !result)
		{
			slFile->Lock();
			dwTag = 0;
			fread(&dwTag, 4, 1, m_file);
			if(feof(m_file)) 
			{
				slFile->Unlock();
				break;
			}
			switch(dwTag)
			{
			case TAG_SIZE:
				fgetpos(m_file, &tp); tp += sizeof(DWORD); fsetpos(m_file, &tp);
				break;
			case TAG_STOREDDATA:
				if(flag)
				{
					fgetpos(m_file, &tp); tp -= 4; fsetpos(m_file, &tp);
					result = TRUE;
				}
				else
				{
					flag = TRUE;
				}
				break;
			case TAG_TIMEMARK:
				fgetpos(m_file, &tp); tp += sizeof(TimeMark); fsetpos(m_file, &tp);
				break;
			case TAG_RMSSTOREDVIDEO_v01:
				dwLen = 0;
				fread(&dwLen, sizeof(DWORD), 1, m_file);
				fread(&m_nFrameRate, sizeof(int), 1, m_file);
				dwRead = fread(m_pData, 1, dwLen-4, m_file);
				if(dwRead != (dwLen - 4)) break;
				if(m_bQuit) break;
				if(dwLen > 0)
				{
					extern BYTE* CMemory;
					enterCritical();
					BYTE** py = getYComponent();
					BYTE** pu = getUComponent();
					BYTE** pv = getVComponent();
					DECOMPRESS_FRAME(*py, *pu, *pv, m_nW, m_nH, m_pData, CMemory);
					leaveCritical();
					if(WaitForRefresh() == 0) 
					{
						slFile->Unlock();
						return FALSE;
					}
				}
				break;
			case TAG_STOREDTIME_v01:
				dwLen = 0;
				fread(&dwLen, sizeof(DWORD), 1, m_file);
				if(dwLen > 50)
				{
					slFile->Unlock();
					return FALSE;
				}
				dwRead = fread(m_szTime, 1, dwLen, m_file);
				if(dwRead != (dwLen)) break;
				enterCritical();
				setSessionTime(m_szTime);
				leaveCritical();
				break;
			case TAG_STOREDDATE_v01:
				dwLen = 0;
				fread(&dwLen, sizeof(DWORD), 1, m_file);

				if(dwLen > 50)
				{
					slFile->Unlock();
					return FALSE;
				}
				dwRead = fread(m_szDate, 1, dwLen, m_file);
				if(dwRead != (dwLen)) break;
				enterCritical();
				setSessionDate(m_szDate);
				leaveCritical();
				break;
			default:
				dwLen = 0;
				fread(&dwLen, sizeof(DWORD), 1, m_file);
				fgetpos(m_file, &tp); tp += dwLen; fsetpos(m_file, &tp);
			}
			slFile->Unlock();	
		}
		if((feof(m_file))) 
		{
			clearerr(m_file);
			if(m_dwCommand != COMMAND_STOP)
			{
				SetMode(COMMAND_STOP);
			}
			return TRUE;
		}

		slFile->Lock();
		fgetpos(m_file, &tp); tp -= 4; fsetpos(m_file, &tp);
		fread(&dwLen, sizeof(DWORD), 1, m_file);
		fgetpos(m_file, &tp); tp -= sizeof(DWORD)*2 + dwLen; fsetpos(m_file, &tp);
		slFile->Unlock();
		result = TRUE;
	}
	else
	{
		slFile->Lock();
		fgetpos(m_file, &tp); tp -= 4; fsetpos(m_file, &tp);
		fread(&dwLen, sizeof(DWORD), 1, m_file);
		fgetpos(m_file, &tp); tp -= sizeof(DWORD)*2 + dwLen; fsetpos(m_file, &tp);
		slFile->Unlock();
		result = TRUE;
	}

	fgetpos(m_file, &m_lCurrentPlayData);

	m_nTotalSteps++;
	return result;
}

DWORD CVideoPlayer::GetMode()
{
	return m_dwCommand;
}

DWORD CVideoPlayer::WaitForRefresh()
{
	CEvent* pevent = getRefreshEvent();
	if(pevent) ::WaitForSingleObject(*pevent, INFINITE);
	if (m_bQuit) return 0;
	HWND hwnd = getWindow();
	if(hwnd)
	{
		PostMessage(hwnd, WM_TIMER, TIMER_REFRESH_DATA, 0L);
	}
	if (m_bQuit) return 0;
	return 1;
}

void CVideoPlayer::outAudio(BYTE* pSrc, int szAudio)
{
	m_criticalAction.Lock();
	m_byPacketNumber = *pSrc;
	m_byPacketNumberStart = m_byPacketNumber;

	pSrc += sizeof(BYTE);
	m_criticalAction.Unlock();
	szAudio -= sizeof(BYTE);

	// could be large packet with few 33 bytes packet
	while(!m_bQuit && szAudio)
	{
		m_criticalAction.Lock();
		m_Audio[m_byPacketNumber].SetPacket(m_byPacketNumber, WAVE_SIZE*2, pSrc, 33);
		m_criticalAction.Unlock();

		szAudio -= 33;
		pSrc += 33;
		// increase packet number for multi-packet case
		m_eventSync.SetEvent();

		m_criticalAction.Lock();
		if(m_bSync == 0)
		{
			if(m_byPacketNumber == PACKET_SYNC)
			{
				m_bSync = 1;
//				SETAUDIOSTATUS(AUDIO_STATUS_PLAY);
			}
			else
			{
//				SETAUDIOSTATUS(AUDIO_STATUS_BUFFERING);
				m_bSync = 0;
			}
		}
		if(szAudio)
		{
			m_byPacketNumber++;
			if(m_byPacketNumber >= PACKET_COUNT) m_byPacketNumber = 0;
		}
		m_criticalAction.Unlock();
	}
}

#endif //def VC70
